//
//  NSString+Extension.m
//  togetherLBS
//
//  Created by Phyllis Hu on 15/7/17.
//  Copyright (c) 2015年 celi. All rights reserved.
//

#import "NSString+Extension.h"

@implementation NSString (Extension)


+(BOOL)isIncludeSpecialCharact: (NSString *)str {
    //***需要过滤的特殊字符：~￥#&*<>《》()[]{}【】^@/￡¤￥|§¨「」『』￠￢￣~@#￥&*（）——+|《》$_€。
    NSRange urgentRange = [str rangeOfCharacterFromSet: [NSCharacterSet characterSetWithCharactersInString: @"~￥#&*<>《》()[]{}【】^@/￡¤￥|§¨「」『』￠￢￣~@#￥&*（）——+|《》$_€"]];
    if (urgentRange.location == NSNotFound)
    {
        return NO;
    }
    return YES;
}

//屏蔽表情符号
+(NSString *)disable_emoji:(NSString *)text
{
    NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:@"[^\\u0020-\\u007E\\u00A0-\\u00BE\\u2E80-\\uA4CF\\uF900-\\uFAFF\\uFE30-\\uFE4F\\uFF00-\\uFFEF\\u0080-\\u009F\\u2000-\\u201f\r\n]" options:NSRegularExpressionCaseInsensitive error:nil];
    NSString *modifiedString = [regex stringByReplacingMatchesInString:text
                                                               options:0
                                                                 range:NSMakeRange(0, [text length])
                                                          withTemplate:@""];
    return modifiedString;
}

+ (NSString*)CLNSLocalizedString:(NSString*) str{ //
    NSString * tempStr = NSLocalizedString(str, nil);
    return tempStr;
}

/**
 * 字符串中是否存在表情
 */
+ (BOOL)isExpression:(NSString *)text{
    
    NSString *string =  [self disable_emoji: text];
    if(string.length != text.length){
        return YES;
    }
    return NO;
}


/**
 *  判断是否有特殊字符或字符为空
 */
+ (BOOL)isSpecialStringOrNoString:(NSString *)contentStr{
    
    if ((([NSString isIncludeSpecialCharact:contentStr])||[NSString isExpression:contentStr]||(contentStr.length < 1))) {
        
        UIAlertView *alert = [[UIAlertView alloc]initWithTitle:nil message:CLLocalizedString(@"noString", nil) delegate:nil cancelButtonTitle:CLLocalizedString(@"iKnow", nil) otherButtonTitles:nil, nil];
        [alert show];
        return YES;
    }
    return NO;
}

/**
 *  字符串转Byte
 */
+ (void)stringToHex:(NSString *)str hexData:(Byte*)hexData writeLenght:(unsigned int *)lenght{
    
    NSArray *array = [str componentsSeparatedByString:@" "];
    
    int index = 0;
    for (NSString *str in array) {
        
        if ((![str isEqualToString:@""])&&(![str isEqualToString:@" "])) {
            hexData[index] = [str intValue];
            index ++;
        }
    }
    *lenght = index;
}


/**
 * byte数组转换成字符串
 * 例子:
 * dataStr[5] = {0x30,x31,x32,x33,x34}
 * return 结果我 @“01234”
 */
+ (NSString*)byteArrayToString:(Byte *)dataStr length:(NSInteger)length{
    
    NSString *str = nil;
    
    for (int i=0; i<length; i++) {
        if (str == nil) {
            str = [NSString stringWithFormat:@"%c",dataStr[i]];
        } else {
            str = [NSString stringWithFormat:@"%@%c",str,dataStr[i]];
        }
    }
    
    return str;
}


@end
